package dataAccessLayer;

import java.util.List;

import org.hibernate.Query;

import connection.Connector;
import entities.Category;
import entities.Comment;
import entities.Picture;
import entities.User;

public class DataManipulator extends Connector {
	public DataManipulator() {
		super();
	}

	public User createNewUser(String userName, String password) {
		Query query = session.createQuery("from User");
		List<User> users = query.list();
		if (users != null) {
			for (int i = 0; i < users.size(); i++) {
				if (users.get(i).getUserName() != null) {
					if (users.get(i).getUserName().equals(userName)) {
						System.out.println("User already exists.");
						return null;
					}
				}
			}
		}
		User user = new User();
		user.setUserName(userName);
		user.setUserPassword(password);
		user.setCategories(null);
		session.saveOrUpdate(user);
		Connector.commitTransaction();
		System.out.println("Saved");
		return user;
	}

	// !
	public Comment createNewComment(String pictureName, String text) {
		Comment comment = new Comment();
		DataGetter dg = new DataGetter();
		comment.setText(text);
		comment.setPicture(dg.getPictureByName(pictureName));
		session.saveOrUpdate(comment);
		Connector.commitTransaction();
		System.out.println("Saved");
		return comment;
	}

	public Category createNewCategory(String categoryName, String parentName,
			String userName) {
		Category category = new Category();
		Query query = session
				.createQuery("from Category c where c.User.UserName=:userName");
		List<Category> categories = query.list();
		for (int i = 0; i < categories.size(); i++) {
			if (categories.get(i).getCategoryName().equals(categoryName)) {
				System.out.println("Category already exists.");
				return null;
			}
		}
		category.setCategoryName(categoryName);
		category.setPictures(null);
		DataGetter dg = new DataGetter();
		category.setUser(dg.getUserByName(userName));
		category.setParent(dg.getCategoryByName(parentName));

		org.hibernate.Transaction trans = session.beginTransaction();
		session.saveOrUpdate(category);
		Connector.commitTransaction();
		System.out.println("Saved");
		return category;
	}

	public Picture createNewPicture(String pictureName, String categoryName) {
		Picture picture = new Picture();
		Query query = session.createQuery("from Picture");
		List<Picture> pictures = query.list();
		for (int i = 0; i < pictures.size(); i++) {
			if (pictures.get(i).getPictureName().equals(pictureName)) {
				System.out.println("Picture already exists.");
				return null;
			}
		}
		picture.setPictureName(pictureName);
		DataGetter dg = new DataGetter();
		picture.setCategory(dg.getCategoryByName(categoryName));
		DataManipulator dm = new DataManipulator();
		picture.setComments(null);
		session.saveOrUpdate(picture);
		Connector.commitTransaction();
		System.out.println("Saved");
		return picture;
	}

	public User editUser(String userName, String newUserName,
			String newUserPassword) {
		Query query = session
				.createQuery("from User u where u.UserName=:userName");
		query.setString("userName", userName);
		query.setMaxResults(1);
		User user = (User) query.uniqueResult();
		if (user != null) {
			Query query2 = session.createQuery("from User");
			List<User> users = query.list();
			if (users != null) {
				for (int i = 0; i < users.size(); i++) {
					if (users.get(i).getUserName() != null) {
						if (users.get(i).getUserName().equals(newUserName)) {
							System.out.println("User already exists.");
							return null;
						}
					}
				}
			}
			user.setUserName(newUserName);
			user.setUserPassword(newUserPassword);
			session.update(user);
			Connector.commitTransaction();
			System.out.println("Updated");
			return user;
		}
		return null;
	}

	public Category editCategory(String categoryName, String newCategoryName) {
		Query query = session
				.createQuery("from Category c where c.CategoryName=:categoryName");
		query.setString("categoryName", categoryName);
		query.setMaxResults(1);
		Category category = (Category) query.uniqueResult();
		if (category != null) {
			Query query2 = session.createQuery("from Category");
			List<Category> categories = query.list();
			if (categories != null) {
				for (int i = 0; i < categories.size(); i++) {
					if (categories.get(i).getCategoryName() != null) {
						if (categories.get(i).getCategoryName().equals(
								newCategoryName)) {
							System.out.println("Category already exists.");
							return null;
						}
					}
				}
			}
			category.setCategoryName(newCategoryName);
			session.update(category);
			Connector.commitTransaction();
			System.out.println("Updated");
			return category;
		}
		return null;
	}

	public Picture editPicture(String pictureName, String newPictureName) {
		Query query = session
				.createQuery("from Picture p where p.PictureName=:pictureName");
		query.setString("pictureName", pictureName);
		query.setMaxResults(1);
		Picture picture = (Picture) query.uniqueResult();
		if (picture != null) {
			Query query2 = session.createQuery("from Picture");
			List<Picture> pictures = query.list();
			for (int i = 0; i < pictures.size(); i++) {
				if (pictures.get(i).getPictureName().equals(newPictureName)) {
					System.out.println("Picture already exists.");
					return null;
				}
			}
			picture.setPictureName(newPictureName);
			session.update(picture);
			Connector.commitTransaction();
			System.out.println("Updated");
			return picture;
		}
		return null;
	}

	public void deleteUser(String userName) {
		Query query = session.createQuery(
				"from User u where u.UserName=:userName");
		query.setString("userName", userName);
		query.setMaxResults(1);
		User user = (User) query.uniqueResult();
		if (user != null) {
			deleteCategoriesByUser(userName);
			session.delete(user);
			System.out.println("Deleted");
		}
		Connector.commitTransaction();
	}

	public void deleteCategoriesByUser(String userName) {
		Query query = session
				.createQuery("from Category c where c.User.UserName=:userName");
		query.setString("userName", userName);
		List<Category> categories = query.list();
		if (categories != null) {
			for (int i = 0; i < categories.size(); i++) {
				deletePicturesByCategory(categories.get(i).getCategoryName());
				session.delete(categories.get(i));
			}
		}
		System.out.println("Deleted");

	}

	public void deletePicturesByCategory(String categoryName) {
		Query query = session
				.createQuery("from Picture p where p.Category.CategoryName=:categoryName");
		query.setString("categoryName", categoryName);
		List<Picture> pictures = query.list();
		if (pictures != null) {
			for (int i = 0; i < pictures.size(); i++) {
				deleteCommentByPicture(pictures.get(i).getPictureName());
				session.delete(pictures.get(i));
			}
		}
		System.out.println("Deleted");

	}

	public void deleteCommentByPicture(String pictureName) {
		Query query = session
				.createQuery("from Comment c where c.Picture.PictureName=:pictureName");
		query.setString("pictureName", pictureName);
		List<Comment> comments = query.list();
		if (comments != null) {
			for (int i = 0; i < comments.size(); i++) {
				session.delete(comments.get(i));
			}
		}
		System.out.println("Deleted");
	}
}
